package com.example.speeddating.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.speeddating.annotaion.LoginUser;
import com.example.speeddating.annotaion.TokenCheck;
import com.example.speeddating.common.BaseResponse;
import com.example.speeddating.common.RespGenerator;
import com.example.speeddating.entity.Bullet;
import com.example.speeddating.entity.TokenEntity;
import com.example.speeddating.enums.ResponseCodeEnum;
import com.example.speeddating.exception.CommonException;
import com.example.speeddating.service.BulletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.HashMap;

/**
 * @author tina
 * @since 2023-08-07 12:03:44
 */
@RestController
public class BulletController {

    @Autowired
    private BulletService bulletService;

    @TokenCheck(roles = "user")
    @GetMapping("/user/bullets")
    public BaseResponse<Page> bullets(@LoginUser TokenEntity tokenEntity, @RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(defaultValue = "1") Integer pageNum){
        Page<Bullet> bulletPage = bulletService.recentBullets(pageNum,pageSize);
        return RespGenerator.returnOK(bulletPage);
    }


    @TokenCheck(roles = "user")
    @PostMapping("/user/wish")
    public BaseResponse<Bullet> wish(@LoginUser TokenEntity tokenEntity, @RequestBody HashMap<String,Object> jsonString){
        if (jsonString.get("content")==null){
            throw new CommonException(ResponseCodeEnum.CODE_600);
        }
        Bullet bullet = bulletService.wish(tokenEntity.getId(),jsonString.get("content").toString());
        return RespGenerator.returnOK(bullet);
    }

}
